package com.edou.system.controller.admin;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.simple.parser.JSONParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.edou.base.controller.BaseController;
import com.edou.base.form.ImageCropForm;
import com.edou.base.util.ImageIm4Util;
import com.edou.base.util.ImageUtil;

/**
 * 图片上传
 * @author lining
 *
 */
@Controller("sysPhotoController")
@Scope("prototype")
@RequestMapping(value = "/admin/photo")
public class SysPhotoController extends BaseController{
	private static final Log logger = LogFactory.getLog(SysPhotoController.class); 
	@Autowired(required = false)
	private ServletContext servletContext;
	@Value("${image.im4.path:#{null}}")
	private String imageIm4Path;
	
	/**
	 * 跳转到缩略图裁剪页面
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/single.htm", method = {RequestMethod.POST, RequestMethod.GET})
	public ModelAndView imageCrop(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws Exception {
		ModelAndView view = new ModelAndView();
		JSONParser p = new JSONParser();
		view.setViewName("/admin/system/sys_photo_single");
		String postData = (String)request.getParameter("postData");
		view.addObject("postData",p.parse(postData));
		return view;
	}
	
	/**
	 * 保存头像
	 * @param model
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/save_photo.htm",method = RequestMethod.POST)
	public ModelAndView add_picture(@ModelAttribute ImageCropForm cropModel, HttpServletRequest request,HttpSession session){
		try{
			String fileRoot = (String)servletContext.getAttribute("fileRoot");
			String realPath = getRealPath(fileRoot, request);
			// 得到文件路径
			String src = cropModel.getPhoto();
			// 得到文件名
			String fileName = src.substring(0, src.indexOf("."));
			// 得到文件的后缀名
			String fileExt = src.substring(src.indexOf("."));
			// 目标地址
			String photoCut = fileName + "_C" + fileExt;
			// 800*80 = _XXL
			String photoLargeXX = fileName + "_XXL" + fileExt;
			// 400*400 = _XL
			String photoLargeX = fileName + "_XL" + fileExt;
			// 200*200 = _L
			String photoLarge = fileName + "_L" + fileExt;
			// 100*100 = _M
			String photoMiddle = fileName + "_M" + fileExt;
			// 50*50 = _S
			String photoSmall = fileName + "_S" + fileExt;
			int[] sourceWAndH = ImageUtil.getImageWAndH(realPath + src);
			// 等比缩放后结果
			double x = sourceWAndH[0] / (double)cropModel.getBroundX() * cropModel.getX();
			double y = sourceWAndH[1] / (double)cropModel.getBroundY() * cropModel.getY();
			double w = sourceWAndH[0] / (double)cropModel.getBroundX() * cropModel.getW();
			double h = sourceWAndH[1] / (double)cropModel.getBroundY() * cropModel.getH();
			//判断当前是否是使用第三方裁剪工具
			if(StringUtils.isBlank(imageIm4Path)){
				// 裁剪图片
				ImageUtil.cutImage(realPath + src,realPath + photoCut,(int)x,(int)y,(int)w,(int)h,fileExt.substring(1).toLowerCase());
				// 缩放得到最大图片
				ImageUtil.zoomImage(realPath + photoCut, realPath + photoLargeXX, 800, 800);
				// 缩放得到超大图片
				ImageUtil.zoomImage(realPath + photoCut, realPath + photoLargeX, 400, 400);
				// 缩放得到大图片
				ImageUtil.zoomImage(realPath + photoCut, realPath + photoLarge, cropModel.getWidth(), cropModel.getHeight());
				// 缩放中等图片
				ImageUtil.zoomImage(realPath + photoLarge, realPath + photoMiddle, cropModel.getWidth()/2, cropModel.getHeight()/2);
				// 缩放小图片
				ImageUtil.zoomImage(realPath + photoLarge, realPath + photoSmall, cropModel.getWidth()/4, cropModel.getHeight()/4);
			}else{
				// 裁剪图片
				ImageIm4Util.cutImage(realPath + src,realPath + photoCut,(int)x,(int)y,(int)w,(int)h,imageIm4Path);
				// 缩放得到最大图片
				ImageIm4Util.zoomImage(realPath + photoCut, realPath + photoLargeXX, 800, 800, imageIm4Path);
				// 缩放得到超大图片
				ImageIm4Util.zoomImage(realPath + photoCut, realPath + photoLargeX, 400, 400, imageIm4Path);
				// 缩放得到大图片
				ImageIm4Util.zoomImage(realPath + photoCut, realPath + photoLarge, cropModel.getWidth(), cropModel.getHeight(), imageIm4Path);
				// 缩放中等图片
				ImageIm4Util.zoomImage(realPath + photoLarge, realPath + photoMiddle, cropModel.getWidth()/2, cropModel.getHeight()/2, imageIm4Path);
				// 缩放小图片
				ImageIm4Util.zoomImage(realPath + photoLarge, realPath + photoSmall, cropModel.getWidth()/4, cropModel.getHeight()/4, imageIm4Path);
			}
			return ajaxJSON(Status.success,src);
		}catch(Exception e){
			String msg = "裁剪图片发生异常:" + e.getMessage();
			logger.error(msg, e);
			return ajaxJSON(Status.error);
		}
	}
	
	
	
}
